var metro = (function(start, end) {
   var goPrev = function(station) {
      var prevStation = kharkov.line[station.line][station.prev];
      return prevStation;
   };
   var goNext = function(station) {
      var nextStation = kharkov.line[station.line][station.next];
      return nextStation;
   };
   var getFirstTransfer = function(station, endLine) {
      var line = kharkov.line[station.line];
      var index = station.index;
      for (var i in line.transfers) {
         if (line.transfers[i].line === endLine) {
            if (index >= i) {
               return goPrev;
            } else if (index <= i) {
               return goNext;
            } else {
               return null;
            }
            break;
         } else {
            /**
             * Logic for shorter way
             */
         }
      }
   };
   var next = function (current, start, end) {
      if (current.next || current.prev) {
         result.push(current.name);
         if (current.transfer && current.transfer.line == end.line) {
            inLine(kharkov.line[current.transfer.line][current.transfer.id], current, end);
         } else {
            var action = getFirstTransfer(current, end.line);
            if (action && result[result.length-1] != end.name) {
               current = action(current);
               next(current, start, end);
            } else {
               inLine(current, start, end);
            }  
         }
      }
   };
   var inLine = function (current, start, end) {
      if (result.toString().search(current.name) > -1) {
         result.push(current.name);   
      }
      if (start.index > end.index) {
         while (current) {
            result.push(current.name);
            if (current.id != end.id) {
               current = goPrev(current);   
            } else {
               break;
            }
         }
      } else {
         while (current) {
            result.push(current.name);
            if (current.id != end.id) {
               current = goNext(current);   
            } else {
               break;
            }
         }
      }
      return result;
   };
   var start = start;
   var current;
   var end = end;
   var result = [];
   for ( var lineName in kharkov.line) {
      for ( var stationName in kharkov.line[lineName]) {
         if (kharkov.line[lineName][stationName].name === start) {
            start = kharkov.line[lineName][stationName];
            current = start;
         } else if (kharkov.line[lineName][stationName].name === end) {
            end = kharkov.line[lineName][stationName];
         }
      }
   }
   
   if (start.line == end.line) {
      inLine(current, start, end);
      console.log(result)
   } else {
      next(current, start, end);
      console.log(result);
   }
})('Tsentralny Rynok', 'Akademika Pavlova');